//根据身份证号码 获取出生日期
//身份正则表达式
const reg = /^[1-9]\d{5}(19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
export const  getBirthday=(idCard)=>{
     if (idCard.length !== 18) {
        throw new Error('身份证号码长度不正确，应为18位。');
    }
     if (reg.test(idCard)) {
    //获取出生日期
        let birthday = idCard.substring(6, 10) + '-' + idCard.substring(10, 12) + '-' + idCard.substring(12, 14);
        return birthday
     }else{
        throw new Error('身份证号码不合法！')
     }

}
// 根据身份证号码获取年龄
export const  getAge=(idCard)=>{
     if (reg.test(idCard)) {
         //获取出生日期
        let birthday = idCard.substring(6, 10) + '-' + idCard.substring(10, 12) + '-' + idCard.substring(12, 14);
        let year = birthday.substring(0,4)
        let month = birthday.substring(4,6)
        let day = birthday.substring(6,8)
        //计算年龄
        let  now = new Date()
        let currentYear = now.getFullYear()//本年
        let currentMonth = now.getMonth()+1//本月
        let  currentDay = now.getDate()//本日
        let age = currentYear-parseInt(year)-1//年龄
        //如果出生年月日比当前日期小，则年龄+1
        if(parseInt(month)<currentMonth||parseInt(month)==currentMonth&&parent(day)<=currentDay){
        age++
        }
        return age
     }else{
        throw new Error('身份证号码不合法！')
     }

}
 
// 根据身份证号码获取性别
export const getSex=(idCard)=>{
    if (reg.test(idCard)) {
        let sex;
        //获取性别
        if (idCard.substr(16, 1) % 2 == 1) {
        sex = "男"
        } else {
        sex = "女"
        }
        return sex
  }else{
        throw new Error('身份证号码不合法！')
     } 
}


//
